package com.example.module_web.viewholder;

import android.support.annotation.IdRes;
import android.support.annotation.StringRes;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.widget.TextView;

/**
 * Created by ASUS on 2018/3/17.
 */

public class BindViewHolder extends RecyclerView.ViewHolder {
    private static final String TAG = "bug";
    private View mItemView;
    private SparseArray<View> mViews;
    public BindViewHolder(View itemView) {
        super(itemView);
        this.mItemView = itemView;
        this.mViews = new SparseArray<>();
    }

    public <T extends View> T getView(@IdRes int viewId){
        Log.e(TAG, "getView: viewId = "+viewId);
        View view = mViews.get(viewId);
        Log.e(TAG, "getView: view = "+view);
        if(view==null){
            view = mItemView.findViewById(viewId);
            mViews.put(viewId,view);
        }
        return (T) view;
    }

    public BindViewHolder setText(@IdRes int viewId, CharSequence value){
        Log.e(TAG, "setText: value = "+value);
        TextView view = getView(viewId);
        Log.e(TAG, "setText: view = "+view);
        view.setText(value);
        return this;
    }

    public BindViewHolder setText(@IdRes int viewId, @StringRes int strRes){
        TextView view = getView(viewId);
        view.setText(strRes);
        return this;
    }
}
